const db = wx.cloud.database()
const _ = db.command
Page({

  data: {
   // 下拉菜单
//    first: '学校',
//    second: '类型',
//    third: '需求',
//    currentTab: 0,
	  pickerdata: { all: "全部", type: "全部类型" },
	  skip: 0,
	  teams: []
  },
//   tabNav: function(e){
//     this.setData({
//       currentTab: e.currentTarget.dataset.current
//     })
//   },

//   hidenav:function(e){
//       if(this.data.displays=="none")
//       {
//         this.setData({
//           displays: "block",
//         })
//       }
//       else
//       {
//         this.setData({
//           displays: "none",
//         })
//       } 
//     },

//   hideNav: function () {
//     this.setData({
//       displays: "none"
//     })
//   },

  JumponNewTeam:function(){
    wx.navigateTo({
      url: '../newTeam/newTeam',
    })
  },
	getPicker(e) {
		console.log(e.detail)
		let pickerdata = e.detail
		let area = ''
		if (pickerdata.all != undefined) {
			area = pickerdata.all
		}
		if (pickerdata.province != undefined) {
			area = pickerdata.province
		}
		if (pickerdata.city != undefined) {
			area = pickerdata.city
		}
		if (pickerdata.school != undefined) {
			area = pickerdata.school
		}
		this.setData({
			skip: 0,
			area: area
		})

		if (pickerdata.type == "全部类型") {
			//全部类
			db.collection('team').where({
				area: area
			}).limit(18).skip(this.data.skip).get().then(res => {
				let teams = res.data
				this.setData({
					skip: 18,
					teams: teams,
					pickerdata: e.detail
				})
			})
			return
		} else {
			//单个类
			db.collection('team').where({
				area: area,
				type: pickerdata.type
			}).limit(18).skip(this.data.skip).get().then(res => {
				console.log(res)
				let teams = res.data
				this.setData({
					skip: 18,
					teams: teams,
					pickerdata: e.detail
				})
			})
			return
		}
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		if (this.data.pickerdata.type == "全部类型") {
			//全部类
			db.collection('team').where({
				area: this.data.area
			}).limit(18).skip(this.data.skip).get().then(res => {
				let teams = this.data.teams.concat(res.data)
				this.setData({
					skip: this.data.skip + 18,
					teams: teams
				})
			})
			return
		} else {
			//单个类
			db.collection('team').where({
				area: this.data.area,
				type: this.data.pickerdata.type
			}).limit(18).skip(this.data.skip).get().then(res => {
				console.log(res)
				let teams = this.data.teams.concat(res.data)
				this.setData({
					skip: this.data.skip + 18,
					teams: teams
				})
			})
			return
		}
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {
		if (this.data.pickerdata.all != undefined) {
			db.collection('team').limit(18).skip(this.data.skip).get().then(res => {
				let teams = this.data.teams.concat(res.data)
				this.setData({
					skip: 18,
					teams: teams
				})
			})
			return
		}
	}
})